package com.xinnet.mms.customerstate.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.xinnet.core.dao.impl.BaseDaoImpl;
import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.customerstate.dao.CustomerStateHistDao;
import com.xinnet.mms.entity.CustomStateHist;
import com.xinnet.mms.entity.Customer;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.Notice;
/**
 * 
 * @author 王湛智
 *
 */

@Repository("customerStateDao")
public class CustomerStateHistDaoImpl extends BaseDaoImpl<Object, Object> implements CustomerStateHistDao {

	/**
	 * 插入一条人员客户关系
	 */
	@Override
	public void addCustomerStateHist(CustomStateHist customerStateHist) throws SQLException {
		this.getSqlSession().insert("CustomStateHist.addCustomerStateHist",customerStateHist);
	}
	
	/**
	 * 根据客户ID获取已接收的客户状态历史
	 * @param customId
	 * @return
	 */
	public List<CustomStateHist> getReceiveCustomStateHistByCustomId(String customId){
		
		List<CustomStateHist> customStateHistList = super.getSqlSession().selectList("CustomStateHist.getReceiveCustomStateHistByCustomId",customId);
		
		return customStateHistList;
	}
	
	/**
	 * 获取客户状态历史
	 * @return
	 */
	public List<CustomStateHist> getCustomStateHist(){
		
		List<CustomStateHist> customStateHistList = super.getSqlSession().selectList("CustomStateHist.getCustomStateHist");
		
		return customStateHistList;	
	}
	
	/**
	 * 根据客户ID 产品ID获取已接收的客户状态历史
	 * @param customId
	 * @param productId
	 * @return
	 */
	public List<CustomStateHist> getReceiveCustomStateHistByCustomIdAndProductId(String customId,String productId){
		CustomStateHist customStateHist = new CustomStateHist();
		customStateHist.setCustomId(customId);
		customStateHist.setProductId(productId);
		List<CustomStateHist> customStateHistList = super.getSqlSession().selectList("CustomStateHist.getReceiveCustomStateHistByCustomIdAndProductId",customStateHist);
		
		return customStateHistList;
		
		
	}
	
	public void updateCustomStateHistByCustomIdAndEmpNo(String customId,String empNo,String productId){
		
		CustomStateHist customStateHist = new CustomStateHist();
		
		customStateHist.setCustomId(customId);
		customStateHist.setEmpId(empNo);
		customStateHist.setProductId(productId);
		
		super.getSqlSession().update("CustomStateHist.updateCustomStateHistByCustomIdAndEmpNo", customStateHist);
		
	}


	/**
	 * 修改客户关系历史表接收状态
	 */
	@Override
	public void updateCustomStateByCustomIdAndEmpno(String cusId,String empno, Integer customerState) {
		CustomStateHist customStateHist=new CustomStateHist();
		customStateHist.setCustomId(cusId);
		customStateHist.setEmpId(empno);
		customStateHist.setCustomState(customerState);
		this.getSqlSession().update("CustomStateHist.updateCustomStateByCustomIdAndEmpno",customStateHist);
	}

	/**
	 * 根据客户ID 销售ID删除客户关系历史
	 */
	@Override
	public void delCustomStateHistByCusIdAndEmpno(String cusId, String empno, String productCode) throws SQLException {
		Customer customer=new Customer();
		customer.setCusId(cusId);
		customer.setEmpno(empno);
		customer.setProductCode(productCode);
		this.getSqlSession().delete("CustomStateHist.delCustomStateHistByCusIdAndEmpno",customer);
	}
	
	/**
	 * 根据业绩保留期 判断客户是否可以进入公海
	 * @param scoreHoldTime
	 * @return
	 */
	public boolean isCanGoOpenSeaByScoreHoldTime(String customId,String scoreHoldTime){
		CustomStateHist customStateHist=new CustomStateHist();
		customStateHist.setCustomId(customId);
		customStateHist.setScoreHoldTime(scoreHoldTime);
		List<CustomStateHist> customStateHistList = this.getSqlSession().selectList("CustomStateHist.isCanGoOpenSeaByScoreHoldTime",customStateHist);
		if(customStateHistList!=null && customStateHistList.size()>0){
			return false;	
		}
		return true;
	}
	
	
	/**
	 * 根据业绩保留期  产品ID  客户ID    获取CustomStateHist
	 * @param customId
	 * @param scoreHoldTime
	 * @param productId
	 * @return
	 */
	public List<CustomStateHist> getCustomStateHistByScoreHoldTimeAndCustomIdAndProductId(String customId,String scoreHoldTime,String productId){
		
		CustomStateHist customStateHist=new CustomStateHist();
		customStateHist.setCustomId(customId);
		customStateHist.setScoreHoldTime(scoreHoldTime);
		customStateHist.setProductId(productId);
		List<CustomStateHist> customStateHistList = this.getSqlSession().selectList("CustomStateHist.getCustomStateHistByScoreHoldTimeAndCustomIdAndProductId",customStateHist);
		return customStateHistList;
	}

	@Override
	public List<CustomStateHist> getCustomStateHistByCustomId(PageView<CustomStateHist> pageView, String customId) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("paging", pageView);
		map.put("customId", customId);
		return super.getSqlSession().selectList("CustomStateHist.queryCustomStateHistByCustomId", map);
	}

	/**
	 * 用pushHistId修改状态历史表
	 */
	@Override
	public void updateCustomStateHistByPushHistId(String pushHistId) throws SQLException {
		this.getSqlSession().update("CustomStateHist.updateCustomStateHistByPushHistId",pushHistId);
	}

	/**
	 * 根据ID修改状态历史表
	 */
	@Override
	public void updateCustomStateHistByCshId(CustomStateHist customStateHist) throws SQLException {
		this.getSqlSession().update("CustomStateHist.updateCustomStateHistByCshId",customStateHist);
	}
	/**
	 * 根据时间区间查询关系表记录
	 */
	@Override
	public List<CustomStateHist> getCshByTime(Notice notice) {
		return this.getSqlSession().selectList("CustomStateHist.getCshByTime",notice);
	}

	@Override
	public List<CustomStateHist> getCshByTimeAndEmpno(Emp emp) {
		
		return this.getSqlSession().selectList("CustomStateHist.getCshByTimeAndEmpno",emp);
	}

	/**
	 * 根据时间区间和客户类型查询关系表记录
	 */
	@Override
	public List<CustomStateHist> getCshByTimeAndCustomerType(Customer customer) {
		return this.getSqlSession().selectList("CustomStateHist.getCshByTimeAndCustomerType",customer);
	}

	@Override
	public CustomStateHist getCustomStateHistByCondition(
			CustomStateHist customerStateHist) {
		return this.getSqlSession().selectOne("CustomStateHist.getCustomStateHistByCondition",customerStateHist);
	}
	
	
}
